package com.game.modular.game.battle.process.skillEffectHandle.base;

import com.game.modular.game.battle.panel.Battle;
import com.game.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.game.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.game.modular.game.battle.panel.skillEffect.domain.BaseTrigger;
import com.game.modular.game.battle.panel.skillEffect.domain.SkillEffectStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;
import java.util.OptionalInt;

import static com.game.modular.game.battle.info.FiledName.phaseMap;

/**
 * 判定
 * 触发|状态(新增|刷新|过期)
 */
@Slf4j
@Component
public class EffectHandle2 {
    @Resource
    private PlayerHeroAttributeServiceImpl attributeService;

    /**
     * 判定
     * 触发|状态(新增|刷新|过期)
     */
    public void tryTriggerJudgment(PvpSkillEffect skillEffect) {
        SkillEffectStatus status = skillEffect.getStatus();
        status.setIsTrigger(1);

        Long battleId = skillEffect.getBattleId();
        Battle battle = Battle.get(battleId);
        Long sourcePlayerHeroId = skillEffect.getSourcePlayerHeroId();
        Long targetPlayerHeroId = skillEffect.getTargetPlayerHeroId();
        Long coreSkillStarId = skillEffect.getSourceCoreSkillStarId();

        PlayerHeroAttribute sourceAttribute = attributeService.get(battleId, sourcePlayerHeroId);
        PlayerHeroAttribute targetAttribute = attributeService.get(battleId, targetPlayerHeroId);

        BaseTrigger trigger = skillEffect.getBaseTrigger();

        List<Integer> integers = skillEffect.obtainNumberOfEffectiveRounds();
        OptionalInt min = integers.stream().mapToInt(Integer::intValue).min();
        OptionalInt max = integers.stream().mapToInt(Integer::intValue).max();
        if (max.isPresent()) {
            if (trigger.getAppoint() < battle.getRound()) {
                status.setStatus(4);
                status.setStatus4Value(max.getAsInt());
            }
        } else {
            throw new RuntimeException("未找到最大值");
        }


        Integer trigger1 = trigger.getTrigger1();
        if (trigger1 >= 1) {
            Integer sourceSkillBattleCountNum = sourceAttribute.getSkillBattleCount().get(skillEffect.getSourceCoreSkillStarId());
            if (!sourceSkillBattleCountNum.equals(trigger1)) {
                status.setIsSave(1);
                status.setIsTrigger(0);
                status.triggerFailReasons.add(-1 + ":非战斗的指定数");
            }
        }

        Integer trigger2 = trigger.getTrigger2();
        if (trigger2 >= 1) {
            Integer sourceSkillRoundCountNum = sourceAttribute.getSkillRoundCount().get(skillEffect.getSourceCoreSkillStarId());
            if (!sourceSkillRoundCountNum.equals(trigger2)) {
                status.setIsSave(1);
                status.setIsTrigger(0);
                status.triggerFailReasons.add(-2 + ":非触发的回合数");
            }
        }
        Integer trigger3 = trigger.getTrigger3();
        if (trigger3 >= 1) {
            Integer sourceAttributePhase = sourceAttribute.getPhase();
            if (!sourceAttributePhase.equals(trigger3)) {
                status.setIsSave(1);
                status.setIsTrigger(0);
                String res = -3 + ":非" + phaseMap.get(trigger3) + "阶段";
                status.triggerFailReasons.add(res);
            }
        }

        List<Integer> trigger4 = trigger.getTrigger4();
        if (!trigger4.isEmpty()) {
            Map<Integer, String> event = sourceAttribute.getEvent();
            event.forEach((skillType, value)->{
                if (!trigger4.contains(skillType)) {
                    status.setIsSave(1);
                    status.setIsTrigger(0);
                    String res = -5 + ":非匹配战法," + skillType+":" + value;
                    status.triggerFailReasons.add(res);
                }
            });
        }

        List<Long> trigger5 = trigger.getTrigger5();
        if (!trigger5.isEmpty()) {
            targetAttribute.behaviorMap.forEach((coreSkillEffectTypeId, changeValue) -> {
                if (!trigger5.contains(coreSkillEffectTypeId)) {
                    status.setIsSave(1);
                    status.setIsTrigger(0);
                    String res = -5 + ":非匹配行为," + coreSkillEffectTypeId + ":" + changeValue;
                    status.triggerFailReasons.add(res);
                }
            });
        }

        skillEffect.setStatus(status);
    }

    /**
     * 打印失败原因
     */
    public void logTriggerFailReasons(PvpSkillEffect skillEffect) {
        SkillEffectStatus status = skillEffect.getStatus();
        if (status.getIsTrigger() != 1) {
            log.error("----触发失败原因:{}", status.triggerFailReasons);
        }
        if (status.getIsTrigger() == 1) {
            log.info("----触发成功,效果id:{}", skillEffect.getCoreSkillEffectId());
        }
    }
}
